#include <netinet/in.h>  
#include <sys/socket.h>  
#include <fcntl.h>
#include <event2/event.h>
#include <assert.h>  
#include <unistd.h>  
#include <string.h>  
#include <stdlib.h>  
#include <stdio.h>  
#include <errno.h>  
#define MAX_LINE 80
void do_read(evutil_socket_t fd, short events, void *arg);
void do_write(evutil_socket_t fd, short events, void *arg);
char rot13_char(char c)  
{
    if ((c >= 'a' && c <= 'm') || (c >= 'A' && c <= 'M'))  
        return c + 13;  
    else if ((c >= 'n' && c <= 'z') || (c >= 'N' && c <= 'Z'))  
        return c - 13;  
    else  
        return c;  
} 
struct fd_state {  
    char buffer[MAX_LINE];  
    size_t buffer_used;  
  
    size_t n_written;  
    size_t write_upto;  
  
    struct event *read_event;  
    struct event *write_event;  
};  
  
struct fd_state * alloc_fd_state(struct event_base *base, evutil_socket_t fd)  
{  
    struct fd_state *state = malloc(sizeof(struct fd_state));  
    if (!state)  
        return NULL;  
  
    state->read_event = event_new(base, fd, EV_READ|EV_PERSIST, do_read, state);  
    if (!state->read_event)  
    {  
        free(state);  
        return NULL;  
    }  
  
    state->write_event = event_new(base, fd, EV_WRITE|EV_PERSIST, do_write, state);  
    if (!state->write_event)  
    {  
        event_free(state->read_event);  
        free(state);  
        return NULL;  
    }  
  
    state->buffer_used = state->n_written = state->write_upto = 0;  
  
    assert(state->write_event);  
    return state;  
}  
  
void free_fd_state(struct fd_state *state)  
{  
    event_free(state->read_event);  
    event_free(state->write_event);  
    free(state);  
}  
  
void do_read(evutil_socket_t fd, short events, void *arg)  
{  
    struct fd_state *state = arg;  
    char buf[20];  
    int i;  
    ssize_t result;  
    printf("\ncome in do_read: fd: %d, state->buffer_used: %d, sizeof(state->buffer): %d\n", fd, state->buffer_used, sizeof(state->buffer));  
    while (1)
    {
        assert(state->write_event);  
        result = recv(fd, buf, sizeof(buf), 0);  
        if (result <= 0)  
            break;  
        printf("recv once, fd: %d, recv size: %d, recv buff: %s\n", fd, result, buf);
        for (i=0; i < result; ++i)  
        {  
            if (state->buffer_used < sizeof(state->buffer))//如果读事件的缓冲区还未满，将收到的数据做转换  
                state->buffer[state->buffer_used++] = rot13_char(buf[i]);
//              state->buffer[state->buffer_used++] = buf[i];//接收什么发送什么，不经过转换，测试用  
            if (buf[i] == '\n') //如果遇到换行，添加写事件，并设置写事件的大小  
            {  
                assert(state->write_event);  
                event_add(state->write_event, NULL);  
                state->write_upto = state->buffer_used;  
                printf("遇到换行符，state->write_upto: %d, state->buffer_used: %d\n",state->write_upto, state->buffer_used);
            }  
        }  
        printf("recv once, state->buffer_used: %d\n", state->buffer_used);  
}  
  
    //判断最后一次接收的字节数  
    if (result == 0)  
    {  
        free_fd_state(state);  
    }  
    else if (result < 0)  
    {  
        if (errno == EAGAIN) // XXXX use evutil macro  
            return;  
        perror("recv");  
        free_fd_state(state);  
    }  
}  
  
void do_write(evutil_socket_t fd, short events, void *arg)  
{  
    struct fd_state *state = arg;  
  
    printf("\ncome in do_write, fd: %d, state->n_written: %d, state->write_upto: %d\n",fd, state->n_written, state->write_upto);  
    while (state->n_written < state->write_upto)  
    {  
        ssize_t result = send(fd, state->buffer + state->n_written, state->write_upto - state->n_written, 0);  
        if (result < 0) {  
            if (errno == EAGAIN) // XXX use evutil macro  
                return;  
            free_fd_state(state);  
            return;  
        }  
        assert(result != 0);
        state->n_written += result;  
        printf("send fd: %d, send size: %d, state->n_written: %d\n", fd, result, state->n_written);  
    }  
  
    if (state->n_written == state->buffer_used)  
    {  
        printf("state->n_written == state->buffer_used: %d\n", state->n_written);  
        state->n_written = state->write_upto = state->buffer_used = 1;  
        printf("state->n_written = state->write_upto = state->buffer_used = 1\n");  
    }  
  
    event_del(state->write_event);  
}  
  
void do_accept(evutil_socket_t listener, short event, void *arg)  
{  
    struct event_base *base = arg;  
    struct sockaddr_storage ss;  
    socklen_t slen = sizeof(ss);  
    int fd = accept(listener, (struct sockaddr*)&ss, &slen);  
    if (fd < 0)  
    { // XXXX eagain??  
        perror("accept");  
    }  
    else if (fd > FD_SETSIZE)  
    {  
        close(fd); // XXX replace all closes with EVUTIL_CLOSESOCKET */  
    }  
    else  
    {  
        struct fd_state *state;  
        evutil_make_socket_nonblocking(fd);  
        state = alloc_fd_state(base, fd);  
        assert(state); /*XXX err*/  
        assert(state->write_event);  
        event_add(state->read_event, NULL);  
    }  
}  
  
void run(void)  
{  
    evutil_socket_t listener;  
    struct sockaddr_in sin;  
    struct event_base *base;  
    struct event *listener_event;
    base = event_base_new();//初始化libevent  
    if (!base)  
        return; /*XXXerr*/  
  
    sin.sin_family = AF_INET;  
    sin.sin_addr.s_addr = 0;//本机  
    sin.sin_port = htons(8000);  
  
    listener = socket(AF_INET, SOCK_STREAM, 0);  
    evutil_make_socket_nonblocking(listener);
#ifndef WIN32  
    {  
        int one = 1;  
        setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));  
    }  
#endif
    if (bind(listener, (struct sockaddr*)&sin, sizeof(sin)) < 0)  
    {  
        perror("bind");  
        return;
    }
    if (listen(listener, 16)<0)
    {
        perror("listen");
        return;
    }  
    listener_event = event_new(base, listener, EV_READ|EV_PERSIST, do_accept, (void*)base);
    event_add(listener_event, NULL);
    event_base_dispatch(base);
} 

int main(int c, char **v) {
    run();
    return 0;
}  